/*The MIT License

Copyright (c) <2008> <Samir Menon>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#include <iostream>
#include <fstream>
#include "neuron.h"
#include "CNaNeuron.h"
#include "CKNaNeuron.h"
#include "CSpikeSpooler.h"
//#include "CNeuronVisualizer.h"
#include "CCaKNaNeuron.h"
#include "CSynCaKNaNeuron.h"
#include "CDCaKNaNeuron.h"
#include "CNeuronalNetwork.h"

using namespace std;
using namespace bis_neuron;

#define SEED 1
#define RND_SEED time(NULL)	
#define NUM_BG_NEURONS 3

int main()
{	
	srand(SEED);
	//NOTE: Clock MUST be created and destroyed in main
	CSystemClock::create_clock();
	CNeuronalNetwork *bg_net;
	int sim_id = 5;
	
	switch(sim_id)
	{
		case 1:
			bg_net = new CNeuronalNetwork(NUM_BG_NEURONS*10,NUM_BG_NEURONS,
											"/home/samir/Desktop/spike_log.txt", 
											"/home/samir/Desktop/data_log.txt");
			bg_net->initialize_ctx2bg();			
			bg_net->run_ctx2bg();
		break;
		
		case 2:
			bg_net = new CNeuronalNetwork(NUM_BG_NEURONS*10,NUM_BG_NEURONS,
											"/home/samir/Desktop/spike_log.txt", 
											"/home/samir/Desktop/data_log.txt");
			bg_net->run_bgbistable();
			bg_net->initialize_bgbistable();	
		break;
		
		case 3:
			bg_net = new CNeuronalNetwork(1,0,
												"/home/samir/Desktop/spike_log.txt", 
												"/home/samir/Desktop/data_log.txt");
			bg_net->initialize_simpleneuron(0.65);
			bg_net->run_simpleneuron();
		break;
		
		case 4:
			bg_net = new CNeuronalNetwork(1,0,
												"/home/samir/Desktop/spike_log.txt", 
												"/home/samir/Desktop/data_log.txt");
			bg_net->initialize_knaneuron(DEFAULT_TAU_K,DEFAULT_DELTA_GK);			 
			bg_net->run_knaneuron();
		break;
		
		case 5:
			bg_net = new CNeuronalNetwork(1,0,
												"/home/samir/Desktop/spike_log.txt", 
												"/home/samir/Desktop/data_log.txt");
			bg_net->initialize_caknaneuron(10,DEFAULT_DELTA_I_CA);			 
			bg_net->run_caknaneuron();
		break;
		
		case 6:
			bg_net = new CNeuronalNetwork(0,1,
												"/home/samir/Desktop/spike_log.txt", 
												"/home/samir/Desktop/data_log.txt");
			bg_net->initialize_bistableneuron();			 
			bg_net->run_bistableneuron();
		break;
	}
	//END Cleanup
	if(NULL != bg_net){	delete bg_net;	}
	CSystemClock::destroy_clock();
}
	/*
	 void init_set_values(float & a_tau_ca, float & a_delta_i_ca, 
										float & a_r_ca,
									 float & a_tau_k, float & a_delta_gk,
									 float & a_sigma, float & a_n, float & a_r)
{
	a_tau_ca = 30;
	a_delta_i_ca = 0.15;
	a_r_ca = 1.0;//resistance of ca channel
	a_tau_k = 100;
	a_delta_gk = 0.5;
	a_sigma = 1.0;
	a_n = 1.0;
	a_r = 1.0;
}

int main()
	{	
	srand(SEED);
	float arg_tau_ca, arg_delta_i_ca, arg_r_ca, arg_tau_k, arg_delta_gk; 
	float arg_sigma, arg_n, arg_r;
//	set_data(arg_tau_ca, arg_delta_i_ca, arg_r_ca, arg_tau_k, arg_delta_gk, 
//						arg_sigma, arg_n, arg_r);
	long i=0 ;
	CSystemClock* main_clock;
	CSystemClock::create_clock();
	main_clock = CSystemClock::get_clock();	
	CSpikeSpooler sp_spool("/home/samir/Desktop/spike_log.txt");
	CSpikeSpooler data_spool("/home/samir/Desktop/data_log.txt");
	
	CDCaKNaNeuron neuron[NUM_BG_NEURONS];
	CSynCaKNaNeuron neuron_ca;
//	CCaKNaNeuron neuron[NUM_NEURONS];
	//CKNaNeuron neuron[NUM_NEURONS];
	//CNaNeuron neuron[NUM_NEURONS];
	
	for(i=0;i<NUM_BG_NEURONS;i++)
	{	
		//neuron[i].initialize(i,3.0,&sp_spool, NULL);
		if(i==0 || i==2){
			neuron[i].initialize(i,NULL, &data_spool);			
		}
		else
		{	neuron[i].initialize(i,NULL, NULL);	}
		//neuron[i].initialize(i,3.0,&sp_spool, &data_spool);
		neuron[i].set_exec_flag_on();
	}
	neuron_ca.initialize(3,NULL, &data_spool);
	neuron_ca.set_exec_flag_on();
	
	neuron[0].set_r(1.35);
	neuron_ca.set_r(1.15);
	neuron[1].set_r(0.7);
	neuron[2].set_r(0.8);
	
	//Syn id, Neuron from
	neuron[2].set_stdp_synapse(0,0);
	neuron[2].set_stdp_synapse(1,1);
	neuron[2].set_stdp_synapse(2,3);//Neuron 3 is the ca neuron
	
	init_set_values(arg_tau_ca, arg_delta_i_ca, arg_r_ca, arg_tau_k, arg_delta_gk, 
							arg_sigma, arg_n, arg_r);
	
	for(arg_tau_ca = 30;i<105;i=i+5) //15 iterations
	{
		neuron_ca.reset_neuron();
		neuron[0].reset_neuron();
		neuron[1].reset_neuron();
		neuron[2].reset_neuron();
		neuron[2].set_data(arg_tau_ca, arg_delta_i_ca, arg_r_ca, arg_tau_k, arg_delta_gk, 
							arg_sigma, arg_n, arg_r);
		for(i=0;i<20000; i++)
		{
			if(neuron_ca.fire()) { neuron[2].activate_da_synapse(2);	}
			if(neuron[0].fire()) { neuron[2].activate_da_synapse(0);	} 
			if(neuron[1].fire()) { neuron[2].activate_da_synapse(1);	}
			neuron[2].fire();
			main_clock->tick();
		}
	}
	
	init_set_values(arg_tau_ca, arg_delta_i_ca, arg_r_ca, arg_tau_k, arg_delta_gk, 
							arg_sigma, arg_n, arg_r);
	
	for(arg_tau_k = 40;i<105;i=i+5) //15 iterations
	{
		neuron[2].reset_neuron();
		neuron[2].set_data(arg_tau_ca, arg_delta_i_ca, arg_r_ca, arg_tau_k, arg_delta_gk, 
							arg_sigma, arg_n, arg_r);
		for(i=0;i<20000; i++)
		{
			if(neuron_ca.fire()) { neuron[2].activate_da_synapse(2);	}
			if(neuron[0].fire()) { neuron[2].activate_da_synapse(0);	} 
			if(neuron[1].fire()) { neuron[2].activate_da_synapse(1);	}
			neuron[2].fire();
			main_clock->tick();
		}
	}
	
	cout<<"Time taken by neural simulation of "<<NUM_BG_NEURONS<<":"<<main_clock->get_time()<<endl;
	CSystemClock::destroy_clock();
	return 0;
}
*/

